package DT42;

import DT8.DT8_Util;
import javagprssrv.Common.DateTime;
import java.text.DecimalFormat;
import javagprssrv.Common.CommonClass;
import java.sql.ResultSet;
import java.util.Map;
import javagprssrv.DataBase.DBSQLExecute;
import javagprssrv.JGS_Param;
import javagprssrv.StructClass.structTask;
import javagprssrv.StructClass.structRecvData;
import org.apache.log4j.Logger;
import org.dusin.commonutil.TheUtil;

public class TrdTCPDataSaver_DT42 extends java.lang.Thread
{
    Logger logger = Logger.getLogger(TrdTCPDataSaver_DT42.class);
    DT42.DT42_CommandStruct DT42CMD;
    javagprssrv.StructClass.structRecvData recvData;
    javagprssrv.StructClass.structTask task;
    int JobIndex;
    java.lang.String _Protocol;
    int _UDPServerSocketIndex;
    java.lang.String _RemoteIP;
    int _RemotePort;
    java.lang.String _Data;
    java.lang.String _OriginRecvData;
    int RecvDataType;
    java.lang.String TaskID;
    int QiShiRi;
    private java.lang.String TCP_TermType;
    int DataType;
    java.lang.String TermNo;
    java.lang.String YBH;
    java.lang.String AsynTelNo;
    java.lang.String AsynTelNo_17;
    java.lang.String DYDJ;
    java.lang.String DYDW;
    java.lang.String zyxs;
    java.lang.String csxs;
    java.lang.String cxxs;
    java.lang.String zdzs;
    java.lang.String zxzs;
    java.lang.String hgs;
    double zdz;
    double zxz;
    double pjz;
    double z00;
    double z01;
    double z02;
    double z03;
    double z04;
    double z05;
    double z06;
    double z07;
    double z08;
    double z09;
    double z10;
    double z11;
    double z12;
    double z13;
    double z14;
    double z15;
    double z16;
    double z17;
    double z18;
    double z19;
    double z20;
    double z21;
    double z22;
    double z23;
    java.lang.String sYB_BL;
    java.lang.String szdz;
    java.lang.String szxz;
    java.lang.String spjz;
    java.lang.String sz00;
    java.lang.String sz01;
    java.lang.String sz02;
    java.lang.String sz03;
    java.lang.String sz04;
    java.lang.String sz05;
    java.lang.String sz06;
    java.lang.String sz07;
    java.lang.String sz08;
    java.lang.String sz09;
    java.lang.String sz10;
    java.lang.String sz11;
    java.lang.String sz12;
    java.lang.String sz13;
    java.lang.String sz14;
    java.lang.String sz15;
    java.lang.String sz16;
    java.lang.String sz17;
    java.lang.String sz18;
    java.lang.String sz19;
    java.lang.String sz20;
    java.lang.String sz21;
    java.lang.String sz22;
    java.lang.String sz23;
    java.lang.String YB_YBH;
    double YB_BL;
    int YB_BBXSW;
    java.lang.String YB_GSMC;
    java.lang.String YB_floatGS;
    java.lang.String s2_Parameter;
    java.lang.String s2_Year;
    java.lang.String s2_Month;
    java.lang.String s2_Date;
    java.lang.String _SJM_DataTime;
    java.lang.String _TJS_ZYXS;
    java.lang.String _TJS_HGS;
    java.lang.String _TJS_CSXS;
    java.lang.String _TJS_CXXS;
    java.lang.String _DYZ_PJZ;
    java.lang.String _DYZ_Min;
    java.lang.String _SJM_MinTime;
    java.lang.String _DYZ_Max;
    java.lang.String _SJM_MaxTime;
    java.lang.String _YB_DT42_XZQYM;
    int _YB_DT42_TermNO;
    int _YB_DT42_LS;
    
    public TrdTCPDataSaver_DT42() {
        super("TrdTCPDataSaver_DT42");
        this.DT42CMD = new DT42.DT42_CommandStruct();
        this.JobIndex = -1;
        this.RecvDataType = -1;
        this.QiShiRi = 20;
        this.TCP_TermType = "42";
        this.TermNo = "";
        this.YBH = "";
        this.AsynTelNo = "";
        this.AsynTelNo_17 = "";
        this.DYDJ = "";
        this.DYDW = "";
        javagprssrv.JGS_Param.myLog.ShowLog("线程 TrdTCPDataSaver_DT42 启动成功。", java.lang.Boolean.TRUE, java.lang.Boolean.TRUE);
    }
    
    private boolean findRecvData() {
        boolean res = false;
       
        synchronized (javagprssrv.JGS_Param.UnSavedRecvData_TCP) {
            for (int i = 0; i < javagprssrv.JGS_Param.UnSavedRecvData_TCP.size(); ++i) {
                if (javagprssrv.JGS_Param.UnSavedRecvData_TCP.elementAt(i).RecvDataType == java.lang.Integer.valueOf(this.TCP_TermType)) {
                    this.recvData = javagprssrv.JGS_Param.UnSavedRecvData_TCP.remove(i);
                    res = true;
                    break;
                }
            }
            if (javagprssrv.JGS_Param.UnSavedRecvData_TCP.size() > 10000) {
                javagprssrv.JGS_Param.UnSavedRecvData_TCP.clear();
            }
        }
       
        if(recvData!=null)
        {
        this._Protocol = this.recvData.Protocol.trim();
        this._RemoteIP = this.recvData.RemoteIP;
        this._RemotePort = this.recvData.RemotePort;
        this._Data = this.recvData.Data;
        this._OriginRecvData = this.recvData.Data;
        }
        
        return res;
    }
    
    @java.lang.Override
	public void run() {
		try {
			while (true) {
				
				//java.lang.System.out.println("接收线程.....");
				//javagprssrv.JGS_Param.myLog.ShowLog("接受到数据", true, true);
			    if (findRecvData())
			    {
					java.lang.System.out.println("接收线程.....1");
					this.DT42CMD.ReadCommand(this._OriginRecvData);
					//javagprssrv.JGS_Param.myLog.ShowLog("接受到数据", true, true);
					if (checkData()) {
						this._YB_DT42_XZQYM = this.DT42CMD.A_A1;
						this._YB_DT42_TermNO = this.DT42CMD.A_A2;
						this._YB_DT42_LS = this.DT42CMD.Pn;
						if (GetYBInfo()) {
							if (this.DT42CMD.AFN == 13) {
								if (this.DT42CMD.Fn == 3) {
									
									javagprssrv.JGS_Param.myLog.ShowLog("五分钟数据协议解析正确", true, true);
									save_5m();
								}
								if (this.DT42CMD.Fn == 33) {
									save_Date();
								}
								if (this.DT42CMD.Fn == 36) {
									save_Month();
								}
							}
							if (this.DT42CMD.AFN == 19) {
								if (this.DT42CMD.Fn == 33) {
									save_Date();
								}
								if (this.DT42CMD.Fn == 36) {
									save_Month();
								}
							}
						}
					}
				}
			    Thread.sleep(200);
			}
		} catch (Exception e) {
			java.lang.System.out.println(e);
			e.printStackTrace();
		}
	}
    
    public boolean checkData() {
        final boolean res = true;
        return res;
    }
    
    public java.lang.String GetPointIDfromDeviceID(java.lang.String _DeviceID) {        
        return new javagprssrv.DataBase.DBSQLExecute().getPointIDByDeviceID(_DeviceID);        
    }
    
    public void save_5m() {
        final int idx = 0;
        java.lang.String s2_Min = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(0, 1));
        final java.lang.String s2_Hour = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(1, 2));
        this.s2_Date = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(2, 3));
        this.s2_Month = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(3, 4));
        this.s2_Year = "20" + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(4, 5));
        final int SJMD = this.DT42CMD.Data.charAt(5);
        final int SJDS = this.DT42CMD.Data.charAt(6);
        final int intStartIdx = 7;
        final java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
        for (int i = 0; i < SJDS; ++i) {
            try {
                java.lang.String _5m_Value = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(intStartIdx + 2 + i * 3, intStartIdx + 2 + 1 + i * 3)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(intStartIdx + 1 + i * 3, intStartIdx + 1 + 1 + i * 3)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(intStartIdx + i * 3, intStartIdx + 1 + i * 3));
                _5m_Value = _5m_Value.substring(0, 3) + "." + _5m_Value.substring(3, 5);
                final double d_zdz = java.lang.Double.valueOf(_5m_Value);
                _5m_Value = df.format(d_zdz * this.YB_BL);
                final java.lang.String _5m_Time = this.s2_Year + "-" + this.s2_Month + "-" + this.s2_Date + " " + s2_Hour + ":" + s2_Min + ":00";
                if (java.lang.Integer.valueOf(s2_Min) == 0) {
                    this.SaveSQL_ZDZ(this.YBH, this.s2_Year, this.s2_Month, this.s2_Date, s2_Hour, _5m_Value);
                }
                this.SaveSQL_5m(this.YBH, this.s2_Year, this.s2_Month, this.s2_Date, s2_Hour, s2_Min, _5m_Value);
            }
            catch (java.lang.Exception ex) {}
            final int sm = java.lang.Integer.valueOf(s2_Min) + SJMD;
            if (sm < 10) {
                s2_Min = "0" + java.lang.String.valueOf(sm);
            }
            else {
                s2_Min = "" + java.lang.String.valueOf(sm);
            }
        }
    }
    
    public void SaveSQL_ZDZ(final java.lang.String DeviceID, final java.lang.String Year, final java.lang.String Month, final java.lang.String Date, final java.lang.String Hour, final java.lang.String DYZ) {
        try {
            javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
            final int recordCounts =dbse .findCountRsj(DeviceID, Year, Month, Date);
            final java.lang.String time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
            final java.lang.String PID = this.GetPointIDfromDeviceID(this.YBH);
            if (recordCounts == 0) {              
                dbse.insertRsj(YB_YBH, PID, s2_Year, s2_Month, s2_Date, time, Hour,DYZ);
            }
            else {
                dbse.updateRsj(YBH,  s2_Year, s2_Month, s2_Date,Hour,DYZ);               
            }
            
        }
        catch (java.lang.Exception ex) {}
    }
    
    public void SaveSQL_5m(final java.lang.String DeviceID, final java.lang.String Year, final java.lang.String Month, final java.lang.String Date, final java.lang.String Hour, final java.lang.String Min, final java.lang.String DYZ) {
        try {            
            final java.lang.String PID = this.GetPointIDfromDeviceID(this.YBH);
            String dataTime = Year + "-" + Month + "-" + Date + " "+Hour + ":" + Min + ":00";
            javagprssrv.JGS_Param.queueFor5m.put(new Object[]{this.YBH,PID,dataTime,DYZ});            
        }
        catch (java.lang.Exception ex) { ex.printStackTrace();}
    }
    
    public void save_Date() {
        final int idx = 0;
        this.s2_Date = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(0, 1));
        this.s2_Month = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(1, 2));
        this.s2_Year = "20" + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(2, 3));
        final int zyxs_0 = this.DT42CMD.Data.charAt(3 + idx);
        final int zyxs_2 = this.DT42CMD.Data.charAt(4 + idx);
        this.zyxs = java.lang.String.valueOf(zyxs_0 + zyxs_2 * 256);
        final int gzs_0 = this.DT42CMD.Data.charAt(5 + idx);
        final int gzs_2 = this.DT42CMD.Data.charAt(6 + idx);
        final java.lang.String gzs = java.lang.String.valueOf(gzs_0 + gzs_2 * 256);
        final int csxs_0 = this.DT42CMD.Data.charAt(7 + idx);
        final int csxs_2 = this.DT42CMD.Data.charAt(8 + idx);
        this.csxs = java.lang.String.valueOf(csxs_0 + csxs_2 * 256);
        final int cxxs_0 = this.DT42CMD.Data.charAt(9 + idx);
        final int cxxs_2 = this.DT42CMD.Data.charAt(10 + idx);
        this.cxxs = java.lang.String.valueOf(cxxs_0 + cxxs_2 * 256);
        final int csxc = this.DT42CMD.Data.charAt(11 + idx);
        final int cxxc = this.DT42CMD.Data.charAt(12 + idx);
        final int hgs_0 = this.DT42CMD.Data.charAt(13 + idx);
        final int hgs_2 = this.DT42CMD.Data.charAt(14 + idx);
        this.hgs = java.lang.String.valueOf(hgs_0 + hgs_2 * 256);
        java.lang.String s_zdz = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(17, 18)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(16, 17)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(15, 16));
        this.zdzs = this.s2_Month + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(20, 21)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(19, 20)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(18, 19));
        java.lang.String s_zxz = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(23, 24)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(22, 23)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(21, 22));
        this.zxzs = this.s2_Month + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(26, 27)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(25, 26)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(24, 25));
        java.lang.String s_pjz = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(29, 30)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(28, 29)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(27, 28));
        s_zdz = s_zdz.substring(0, 3) + "." + s_zdz.substring(3, 5);
        s_zxz = s_zxz.substring(0, 3) + "." + s_zxz.substring(3, 5);
        s_pjz = s_pjz.substring(0, 3) + "." + s_pjz.substring(3, 5);
        final java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
        final double d_zdz = java.lang.Double.valueOf(s_zdz);
        s_zdz = df.format(d_zdz * this.YB_BL);
        final double d_zxz = java.lang.Double.valueOf(s_zxz);
        s_zxz = df.format(d_zxz * this.YB_BL);
        final double d_pjz = java.lang.Double.valueOf(s_pjz);
        s_pjz = df.format(d_pjz * this.YB_BL);
        this.zdzs = this.s2_Year + "-" + this.s2_Month + "-" + this.zdzs.substring(2, 4) + " " + this.zdzs.substring(4, 6) + ":" + this.zdzs.substring(6, 8) + ":00";
        this.zxzs = this.s2_Year + "-" + this.s2_Month + "-" + this.zxzs.substring(2, 4) + " " + this.zxzs.substring(4, 6) + ":" + this.zxzs.substring(6, 8) + ":00";
        try {
            //java.lang.String sql = "select count(*) from T_RSJ where DeviceID='" + this.YBH + "' and dataYear='" + this.s2_Year + "' and dataMonth='" + this.s2_Month + "' and dataDay='" + this.s2_Date + "'";
            javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();

            final int recordCounts = dbse.findCountRsj(YBH, s2_Year, s2_Month, s2_Date);
            final java.lang.String _zyxs = "";
            final java.lang.String time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
            final java.lang.String PID = this.GetPointIDfromDeviceID(this.YBH);
            String sql;
            if (recordCounts == 0) {
                sql = "insert into T_RSJ(dataYear,dataMonth,dataDay,DeviceID,PointID,CreateTime,defaultTotalTime,aboveUpperLimitNumber,underLowerLimitNumber,totalRunMin,AboveUpperLimitMin,underLowerLimitMin,maxVoltage,maxVoltageTime,minVoltage,minVoltageTime,avgVoltage) values (";
                sql = sql + this.s2_Year + "," + this.s2_Month + "," + this.s2_Date + "," + this.YB_YBH + "," + PID + ",'" + time + "',";
                sql = sql + gzs + "," + csxc + "," + cxxc + "," + this.zyxs + "," + this.csxs + "," + this.cxxs + "," + s_zdz + ",'" + this.zdzs + "'," + s_zxz + ",'" + this.zxzs + "'," + s_pjz + ")";
            }
            else {
                sql = "UPDATE T_RSJ set createTime='" + time + "',totalRunMin=" + this.zyxs + ",AboveUpperLimitMin=" + this.csxs + ",underLowerLimitMin=" + this.cxxs + ",maxVoltage=" + s_zdz + ",maxVoltageTime='" + this.zdzs + "',minVoltage=" + s_zxz + ",minVoltageTime='" + this.zxzs + "',avgVoltage=" + s_pjz;
                sql = sql + ",defaultTotalTime=" + gzs + ",aboveUpperLimitNumber=" + csxc + ",underLowerLimitNumber=" + cxxc;
                sql = sql + " WHERE datayear=" + this.s2_Year + " and dataMonth=" + this.s2_Month + " and dataDay=" + this.s2_Date + " and deviceID=" + this.YBH;
            }
            
            this.SQLExecute(sql);
            logger.info(sql);
        }
        catch (java.lang.Exception ex) {}
    }
    
    public void save_Month() {
        final int idx = -1;
        final int m = this.DT42CMD.Data.charAt(1 + idx);
        if (m < 10) {
            this.s2_Month = "0" + m;
        }
        else {
            this.s2_Month = "" + m;
        }
        final int y = this.DT42CMD.Data.charAt(2 + idx);
        if (y < 10) {
            this.s2_Year = "200" + y;
        }
        else {
            this.s2_Year = "20" + y;
        }
        this.s2_Month = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(0, 1));
        this.s2_Year = "20" + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(1, 2));
        final int zyxs_0 = this.DT42CMD.Data.charAt(3 + idx);
        final int zyxs_2 = this.DT42CMD.Data.charAt(4 + idx);
        this.zyxs = java.lang.String.valueOf(zyxs_0 + zyxs_2 * 256);
        final int gzs_0 = this.DT42CMD.Data.charAt(5 + idx);
        final int gzs_2 = this.DT42CMD.Data.charAt(6 + idx);
        final java.lang.String gzs = java.lang.String.valueOf(gzs_0 + gzs_2 * 256);
        final int csxs_0 = this.DT42CMD.Data.charAt(7 + idx);
        final int csxs_2 = this.DT42CMD.Data.charAt(8 + idx);
        this.csxs = java.lang.String.valueOf(csxs_0 + csxs_2 * 256);
        final int cxxs_0 = this.DT42CMD.Data.charAt(9 + idx);
        final int cxxs_2 = this.DT42CMD.Data.charAt(10 + idx);
        this.cxxs = java.lang.String.valueOf(cxxs_0 + cxxs_2 * 256);
        final int csxc_0 = this.DT42CMD.Data.charAt(11 + idx);
        final int csxc_2 = this.DT42CMD.Data.charAt(12 + idx);
        final int csxc = csxc_0 + csxc_2 * 256;
        final int cxxc_0 = this.DT42CMD.Data.charAt(13 + idx);
        final int cxxc_2 = this.DT42CMD.Data.charAt(14 + idx);
        final int cxxc = cxxc_0 + cxxc_2 * 256;
        final int hgs_0 = this.DT42CMD.Data.charAt(15 + idx);
        final int hgs_2 = this.DT42CMD.Data.charAt(16 + idx);
        this.hgs = java.lang.String.valueOf(hgs_0 + hgs_2 * 256);
        java.lang.String s_zdz = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(18, 19)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(17, 18)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(16, 17));
        final int rq_zdzs = java.lang.Integer.valueOf(new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(21, 22)));
        int intMonth = java.lang.Integer.valueOf(this.s2_Month);
        java.lang.String strMonth = "00";
        if (rq_zdzs >= this.QiShiRi) {
            --intMonth;
            if (intMonth <= 0) {
                intMonth = 12;
            }
        }
        if (intMonth > 9) {
            strMonth = java.lang.String.valueOf(intMonth);
        }
        else {
            strMonth = "0" + java.lang.String.valueOf(intMonth);
        }
        this.zdzs = strMonth + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(21, 22)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(20, 21)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(19, 20));
        java.lang.String s_zxz = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(24, 25)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(23, 24)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(22, 23));
        final int rq_zxzs = java.lang.Integer.valueOf(new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(27, 28)));
        intMonth = java.lang.Integer.valueOf(this.s2_Month);
        strMonth = "00";
        if (rq_zxzs >= this.QiShiRi) {
            --intMonth;
            if (intMonth <= 0) {
                intMonth = 12;
            }
        }
        if (intMonth > 9) {
            strMonth = java.lang.String.valueOf(intMonth);
        }
        else {
            strMonth = "0" + java.lang.String.valueOf(intMonth);
        }
        this.zxzs = strMonth + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(27, 28)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(26, 27)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(25, 26));
        java.lang.String s_pjz = new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(30, 31)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(29, 30)) + new javagprssrv.Common.CommonClass()._BCD2STR(this.DT42CMD.Data.substring(28, 29));
        s_zdz = s_zdz.substring(0, 3) + "." + s_zdz.substring(3, 5);
        s_zxz = s_zxz.substring(0, 3) + "." + s_zxz.substring(3, 5);
        s_pjz = s_pjz.substring(0, 3) + "." + s_pjz.substring(3, 5);
        final java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
        final double d_zdz = java.lang.Double.valueOf(s_zdz);
        s_zdz = df.format(d_zdz * this.YB_BL);
        final double d_zxz = java.lang.Double.valueOf(s_zxz);
        s_zxz = df.format(d_zxz * this.YB_BL);
        final double d_pjz = java.lang.Double.valueOf(s_pjz);
        s_pjz = df.format(d_pjz * this.YB_BL);
        //设置最大值时、最小值时的正确日期
        String zdzsYmd = TheUtil.correctDate(this.s2_Year, this.s2_Month, this.zdzs.substring(2, 4));
        String zxzsYmd = TheUtil.correctDate(this.s2_Year, this.s2_Month, this.zxzs.substring(2, 4));
        //this.zdzs = this.s2_Year + "-" + this.s2_Month + "-" + this.zdzs.substring(2, 4) + " " + this.zdzs.substring(4, 6) + ":" + this.zdzs.substring(6, 8) + ":00";
        //this.zxzs = this.s2_Year + "-" + this.s2_Month + "-" + this.zxzs.substring(2, 4) + " " + this.zxzs.substring(4, 6) + ":" + this.zxzs.substring(6, 8) + ":00";
        
        this.zdzs = zdzsYmd + " " + this.zdzs.substring(4, 6) + ":" + this.zdzs.substring(6, 8) + ":00";
        this.zxzs = zxzsYmd + " " + this.zxzs.substring(4, 6) + ":" + this.zxzs.substring(6, 8) + ":00";
        try {
            //java.lang.String sql = "select count(*) from T_YSJ where DeviceID='" + this.YBH + "' and dataYear='" + this.s2_Year + "' and dataMonth='" + this.s2_Month + "'";
            String sql;
            final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
            
            final int recordCounts = dbse.findCountYsj(YBH, s2_Year, s2_Month);
            final java.lang.String _zyxs = "";
            final java.lang.String time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
            final java.lang.String PID = this.GetPointIDfromDeviceID(this.YBH);
            
            
            if (recordCounts == 0) {
                sql = "insert into T_YSJ(dataYear,dataMonth,DeviceID,PointID,CreateTime,totalRunMin,AboveUpperLimitMin,underLowerLimitMin,maxVoltage,maxVoltageTime,minVoltage,minVoltageTime,avgVoltage) values (";
                sql = sql + this.s2_Year + "," + this.s2_Month + "," + this.YB_YBH + "," + PID + ",'" + time + "',";
                sql = sql + this.zyxs + "," + this.csxs + "," + this.cxxs + "," + s_zdz + ",'" + this.zdzs + "'," + s_zxz + ",'" + this.zxzs + "'," + s_pjz + ")";
            }
            else {
                sql = "UPDATE T_YSJ set createTime='" + time + "',totalRunMin=" + this.zyxs + ",AboveUpperLimitMin=" + this.csxs + ",underLowerLimitMin=" + this.cxxs + ",maxVoltage=" + s_zdz + ",maxVoltageTime='" + this.zdzs + "',minVoltage=" + s_zxz + ",minVoltageTime='" + this.zxzs + "',avgVoltage=" + s_pjz;
                sql = sql + " WHERE datayear=" + this.s2_Year + " and dataMonth=" + this.s2_Month + " and deviceID=" + this.YBH;
            }
            this.SQLExecute(sql);
        }
        catch (java.lang.Exception ex) {}
    }
    
    //数据库表结构和当前不符 2021/7/8
    public void save_03() throws java.lang.Exception {
        this.ReadRecvDataGetValue();
        final int MonthDataType = 1;
        java.lang.String sql = "select count(*) from T_YSJ where 仪表号='" + this.YB_YBH + "' and 年月='20" + this.s2_Year + this.s2_Month + "'";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
        rs.next();
        final int recordCounts = rs.getInt(1);
        java.lang.String _zyxs = "";
        final java.lang.String time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
        if (recordCounts == 0) {
            if (java.lang.Integer.valueOf(this.zyxs) == 0) {
                _zyxs = "1";
            }
            else {
                _zyxs = this.zyxs;
            }
            sql = "insert into T_YSJ(年月,仪表号,保存日期,总运行时,超上限时,超下限时,最大值,最大值时,最小值,最小值时,平均值,种类,合格时,合格率,超上限率,超下限率) values (";
            sql = sql + "'20" + this.s2_Year + this.s2_Month + "','" + this.YB_YBH + "','" + time + "',";
            sql = sql + this.zyxs + "," + this.csxs + "," + this.cxxs + "," + this.szdz + ",'" + this.zdzs + "'," + this.szxz + ",'" + this.zxzs + "',";
            sql = sql + this.spjz + "," + java.lang.String.valueOf(MonthDataType) + "," + this.hgs + ",'" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.hgs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "'" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.csxs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "'" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.cxxs) / java.lang.Integer.valueOf(_zyxs)) + "')";
        }
        else {
            if (java.lang.Integer.valueOf(this.zyxs) == 0) {
                _zyxs = "1";
            }
            else {
                _zyxs = this.zyxs;
            }
            sql = "update T_YSJ set 年月='20" + this.s2_Year + this.s2_Month + "',仪表号='" + this.YB_YBH + "',保存日期='" + time + "',";
            sql = sql + "总运行时=" + this.zyxs + ",超上限时=" + this.csxs + ",超下限时=" + this.cxxs + ",最大值=" + this.szdz + ",最大值时='" + this.zdzs + "',最小值=" + this.szxz + ",最小值时='" + this.zxzs + "',平均值=" + this.spjz + ",";
            sql = sql + "超上限率='" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.csxs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "超下限率='" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.cxxs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "合格率='" + new java.text.DecimalFormat(this.YB_floatGS).format(100.0 * java.lang.Integer.valueOf(this.hgs) / java.lang.Integer.valueOf(_zyxs)) + "',";
            sql = sql + "合格时=" + this.hgs + ",";
            sql = sql + "种类=" + java.lang.String.valueOf(MonthDataType) + " where 仪表号='" + this.YB_YBH + "' and 年月='20" + this.s2_Year + this.s2_Month + "'";
        }
        this.SQLExecute(sql);
    }
    //数据库不符，应该在云南没有用到这个方法
    public void save_04() throws java.lang.Exception {
        this.ReadRecvDataGetValue();
        java.lang.String sql = "select count(*) from T_RSJ where 仪表号='" + this.YB_YBH + "' and 日期='20" + this.s2_Year + this.s2_Month + this.s2_Date + "'";
        final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
        rs.next();
        final int recordCounts = rs.getInt(1);
        java.lang.String _zyxs = "";
        final java.lang.String time = new javagprssrv.Common.DateTime().getDateTime("YYYY-MM-DD HH:NN:SS");
        if (recordCounts == 0) {
            if (java.lang.Integer.valueOf(this.zyxs) == 0) {
                _zyxs = "1";
            }
            else {
                _zyxs = this.zyxs;
            }
            sql = "insert into T_RSJ(日期,仪表号,保存日期,总运行时,超上限时,超下限时,最大值,最大值时,最小值,最小值时,平均值,";
            sql += "种类) values (";
            sql = sql + "'20" + this.s2_Year + this.s2_Month + this.s2_Date + "','" + this.YB_YBH + "','" + time + "',";
            sql = sql + this.zyxs + "," + this.csxs + "," + this.cxxs + "," + this.szdz + ",'" + this.zdzs + "'," + this.szxz + ",'" + this.zxzs + "'," + this.spjz + ",";
            sql = sql + java.lang.String.valueOf(this.DataType) + ")";
        }
        else {
            if (java.lang.Integer.valueOf(this.zyxs) == 0) {
                _zyxs = "1";
            }
            else {
                _zyxs = this.zyxs;
            }
            sql = "update T_RSJ set 日期='20" + this.s2_Year + this.s2_Month + this.s2_Date + "',仪表号='" + this.YB_YBH + "',保存日期='" + time + "',";
            sql = sql + "总运行时=" + this.zyxs + ",超上限时=" + this.csxs + ",超下限时=" + this.cxxs + ",最大值=" + this.szdz + ",最大值时='" + this.zdzs + "',最小值=" + this.szxz + ",最小值时='" + this.zxzs + "',平均值=" + this.spjz + ",";
            sql = sql + " 种类=" + java.lang.String.valueOf(this.DataType) + " where 仪表号='" + this.YB_YBH + "' and 日期='20" + this.s2_Year + this.s2_Month + this.s2_Date + "'";
        }
        this.SQLExecute(sql);
    }
    
    public boolean GetYBInfo() {
        boolean res = true;
        //final java.lang.String sql = "select VL.code,device.* from V_pointExtend device left outer join T_VoltageLevel VL on VL.id=device.VoltageLevelID  where device.AdminAreaCode='" + this._YB_DT42_XZQYM + "' and device.TerminalNO='" + this._YB_DT42_TermNO + "' and device.CHANNEL='" + this._YB_DT42_LS + "'";
        
        try {
            final javagprssrv.DataBase.DBSQLExecute dbse = new javagprssrv.DataBase.DBSQLExecute();
        //final java.sql.ResultSet rs = dbse.SQL_Query(0, sql);
            Map<String,Object> ybinfo = dbse.findYbInfo(this._YB_DT42_XZQYM, ""+this._YB_DT42_TermNO, ""+this._YB_DT42_LS);
            this.YB_YBH =String.valueOf( ybinfo.get("deviceId"));
            this.YBH = this.YB_YBH;
            final int code = Integer.parseInt((String)ybinfo.get("vcode"));
            this.YB_BL = 1.0;
            if (code == 2) {
                this.YB_BL = 1.0;
            }
            if (code == 3) {
                this.YB_BL = 1.0;
            }
            if (code == 7) {
                this.YB_BL = 60.0;
            }
            if (code == 8) {
                this.YB_BL = 100.0;
            }
            if (code == 9) {
                this.YB_BL = 200.0;
            }
            if (code == 10) {
                this.YB_BL = 350.0;
            }
            if (code == 12) {
                this.YB_BL = 1100.0;
            }
            if (code == 13) {
                this.YB_BL = 2200.0;
            }
            if (code == 14) {
                this.YB_BL = 3300.0;
            }
            if (code == 15) {
                this.YB_BL = 5000.0;
            }
            if (code == 16) {
                this.YB_BL = 7500.0;
            }
            if (code == 17) {
                this.YB_BL = 10000.0;
            }
            this.YB_BBXSW = 3;
            this.YB_floatGS = "0.";
            for (int i = 0; i < this.YB_BBXSW; ++i) {
                this.YB_floatGS += "0";
            }
            res = true;
        }
        catch (java.lang.Exception e) {
            res = false;
        }
        return res;
    }
    
    public void ReadRecvDataGetValue() throws java.lang.Exception {
        final int idxStart = 0;
        this._SJM_DataTime = this._Data.substring(30, 34);
        this._TJS_ZYXS = this._Data.substring(43, 45);
        this._TJS_HGS = this._Data.substring(45, 47);
        this._TJS_CSXS = this._Data.substring(47, 49);
        this._TJS_CXXS = this._Data.substring(49, 51);
        this._DYZ_PJZ = this._Data.substring(51, 55);
        this._DYZ_Min = this._Data.substring(55, 59);
        this._SJM_MinTime = this._Data.substring(59, 63);
        this._DYZ_Max = this._Data.substring(63, 67);
        this._SJM_MaxTime = this._Data.substring(67, 71);
        this._SJM_DataTime = new DT8.DT8_Util().DT8_GetDatefromSJM(this._SJM_DataTime);
        this._SJM_MinTime = new DT8.DT8_Util().DT8_GetDatefromSJM(this._SJM_MinTime);
        this._SJM_MaxTime = new DT8.DT8_Util().DT8_GetDatefromSJM(this._SJM_MaxTime);
        this.s2_Year = this._SJM_DataTime.substring(2, 4);
        this.s2_Month = this._SJM_DataTime.substring(5, 7);
        this.s2_Date = this._SJM_DataTime.substring(8, 10);
        this.zyxs = java.lang.String.valueOf(this._TJS_ZYXS.charAt(0) + this._TJS_ZYXS.charAt(1) * 'Ā');
        this.hgs = java.lang.String.valueOf(this._TJS_HGS.charAt(0) + this._TJS_HGS.charAt(1) * 'Ā');
        this.csxs = java.lang.String.valueOf(this._TJS_CSXS.charAt(0) + this._TJS_CSXS.charAt(1) * 'Ā');
        this.cxxs = java.lang.String.valueOf(this._TJS_CXXS.charAt(0) + this._TJS_CXXS.charAt(1) * 'Ā');
        this.zxzs = this._SJM_MinTime.substring(5, 7) + this._SJM_MinTime.substring(8, 10) + this._SJM_MinTime.substring(11, 13) + this._SJM_MinTime.substring(14, 16);
        this.zdzs = this._SJM_MaxTime.substring(5, 7) + this._SJM_MaxTime.substring(8, 10) + this._SJM_MaxTime.substring(11, 13) + this._SJM_MaxTime.substring(14, 16);
        this.pjz = this.strDY2doubleDY(this._DYZ_PJZ);
        this.zdz = this.strDY2doubleDY(this._DYZ_Max);
        this.zxz = this.strDY2doubleDY(this._DYZ_Min);
        if (this.DYDW.toUpperCase().equals("KV")) {
            this.YB_BL = 0.001;
        }
        if (this.DYDW.toUpperCase().equals("V")) {
            this.YB_BL = 1.0;
        }
        final java.text.DecimalFormat df = new java.text.DecimalFormat("0.0000");
        this.szdz = df.format(this.zdz * this.YB_BL);
        this.szxz = df.format(this.zxz * this.YB_BL);
        this.spjz = df.format(this.pjz * this.YB_BL);
    }
    
    private double strDY2doubleDY(final java.lang.String strDY) {
        double res = 0.0;
        final int v0 = strDY.charAt(0);
        final int v0_0 = v0 / 16;
        final int v0_2 = v0 % 16;
        final int v2 = strDY.charAt(1);
        final int v1_0 = v2 / 16;
        final int v1_2 = v2 % 16;
        final int v3 = strDY.charAt(2);
        final int v2_0 = v3 / 16;
        final int v2_2 = v3 % 16;
        final int v4 = strDY.charAt(3);
        final int v3_0 = v4 / 16;
        final int v3_2 = v4 % 16;
        res = v3_0 * 100000 + v3_2 * 10000 + v2_0 * 1000 + v2_2 * 100 + v1_0 * 10 + v1_2 * 1 + v0_0 * 0.1 + v0_2 * 0.01;
        return res;
    }
    
    private int getValue(final java.lang.String _data, final int startIndex, final int endIndex) {
        int res = 0;
        final int lo = _data.charAt(startIndex);
        final int hi = _data.charAt(endIndex);
        res = hi * 255 + lo;
        return res;
    }
    
    public void SQLExecute(final java.lang.String sql) {
        synchronized (javagprssrv.JGS_Param.UnSavedDBSQLString) {
            javagprssrv.JGS_Param.UnSavedDBSQLString.add(sql);
        }
    }
}
